var express = require('express');
var httpProxy = require('http-proxy');

//创建express实例
var app = express();
var port = 9010;

app.all('*', function(req, res, next) {
       res.header("Access-Control-Allow-Origin", req.headers.origin);
       res.header('Access-Control-Allow-Credentials', true);
       res.header("Access-Control-Allow-Headers", "Content-Type, X-Requested-With,Content-Length,Authorization,Accept,Cookie,Cache-Control,Pragma,expire-day");
       res.header('Access-Control-Allow-Methods', 'POST, GET, PUT, DELETE, OPTIONS');

       next();
});

var local = {  
  host: '192.168.245.76',
  port: 8089
}

var local1 = {
  host: '172.16.1.160',
  port: 8089
}

var local2 = {
  host: '192.168.1.103',
  port: 8089
}

//代理
var proxy = httpProxy.createProxyServer({
	target: local2
});

// 代理中间件，只代理url包含/webservice的请求
app.use(function(req, res, next) {
        console.log(req.url);
	
    // if (req.url.indexOf('work_plus') > -1 || req.url.indexOf('login') > -1 || req.url.indexOf('accounts') > -1 || req.url.indexOf('files') > -1) {
        proxy.web(req, res);
        console.log(req.url);
    // }
})

app.listen(process.env.PORT || port);

console.log('RandF Proxy!');
console.log('Listening Port:'+ port);
